<template>
<div id="p-main">
    <p style="text-align:center;color:red;">提示：（如果无法显示图片，请点击连接查看或手动复制地址到浏览器中打开）</p>
    <h3 class="photo-title">
        tabId: {{ title }} ，
        length: {{ photoList.length }}
    </h3>
    <div id="p-contents" class="animate">
        <ul>
            <li v-for="(item,i) in photoList" :key="'item.id_'+i">
                <a :href="item.img" target="_blank">
                    <img :src="item.img" :alt="item.img">
                </a>
            </li>
            <li class="more" @click="handleMore">加载更多</li>
        </ul>
    </div>
</div>
</template>

<script>
import { mapState } from "vuex"
export default {
    name: "photoList",
    data() {
        return {
            title: "",
            count: 15,
            page: 4
        }
    },
    beforeRouteEnter (to, from, next) {
        if(to.meta.title) {
            next(vm => {
                vm.title = to.meta.title
            })
        }
    },
    mounted() {
        this.getPhotoList()
    },
    methods: {
        // 点击加载更多图片
        handleMore() {
            if(this.page >= 30) {
                alert("暂无更多数据，敬请更新。。。")
                return;
            }
            this.page++
            this.$store.dispatch("photo/getPhotoList", {
                page: this.page,
                count: this.count
            })
        },

        // 获取美图
        getPhotoList() {
            if(this.$store.state.photo.photoList.length === 0) {
                this.$store.dispatch("photo/getPhotoList", {
                        page: this.page,
                    count: this.count
                })
            }
        }
    },
    computed: {
        // ...mapState('user',['userInfo'])
        // 前面是命名空间。后面是参数
        ...mapState("photo", ['photoList'])
    },
}
</script>

<style scoped>
#p-main {
    height: 100%;
    display: flex;
    flex-direction: column;
}
#p-contents {
    flex: 1;
    overflow: auto;
    margin-bottom: 0;
    position: relative;
    display: flex;
    flex-direction: column;
}
.photo-title {
    border:1px solid red;
    margin:5px 10px 0;
    text-align:center;
    box-shadow: 0 0 8px #bf79f8;
    border-radius: 5px;
}
#p-contents ul {
    word-wrap: break-word;
    padding: 0 15px;
}
#p-contents ul li {
    min-height: 50px;
    border: 1px solid #fc8282;
    padding: 6px 8px;
    margin: 8px 0;
    border-radius: 10px;
    box-shadow: 0 0 5px #bf79f8;
}
#p-contents ul li a>img {
    width: 100%;
    height: 100%;
}
#p-contents .more {
    line-height: 50px;
    text-align: center;
    border: 1px solid #fc8282;
    box-shadow: 0 0 5px #bf79f8;
    font-size: 20px;
    font-weight: bolder;
    color: red;
}
</style>